package com.lgt_enterprise_android.view.pages.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.lgt_enterprise_android.Constant;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.base.BaseActivity;
import com.lgt_enterprise_android.utils.BasicHttp;
import com.lgt_enterprise_android.utils.HttpManager;
import com.lgt_enterprise_android.utils.PrefUtils;
import com.lgt_enterprise_android.utils.StringUtil;
import com.lgt_enterprise_android.view.MainActivity;
import com.widgets.CustomizeTitleView;

public class EditInfoActivity extends BaseActivity {

    public final static int INVIEW_CODE = 1;  //修改邀请码
    public final static int ENTERPRISE_NAME = 2;  //企业名称
    public final static int SHOP_NAME = 3;  //企业名称
    public final static int USER_NAME = 4;  //个人姓名

    public final static int IP = 5;  //个人姓名

    private EditText editText;
    private TextView txtTip;
    private Button btnOk;
    private CustomizeTitleView customizeTitleView;

    private String strOldInviteCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_invite_code);

        editText = (EditText) findViewById(R.id.edit_text);
        txtTip = (TextView) findViewById(R.id.txt_tip);
        customizeTitleView = (CustomizeTitleView) findViewById(R.id.customizeTitleView);

        final int _type = getIntent().getIntExtra("type", 0);

        String val = getIntent().getStringExtra("val");
        btnOk = (Button) findViewById(R.id.btn_ok);
        editText.setText(val);

        if (!StringUtil.isEmpty(val)) {
            editText.setSelection(val.length());
        }

        if (_type == INVIEW_CODE) {
            customizeTitleView.setTitle("填写邀请码");
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});

            strOldInviteCode = val;

            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    txtTip.setText("");
                    if (editText.getText().toString().length() == 6) {
                        btnOk.setEnabled(true);
                        btnOk.setBackground(getDrawable(R.drawable.bg_button));
                    } else {
                        btnOk.setEnabled(false);
                        btnOk.setBackground(getDrawable(R.drawable.bg_button_disable));
                    }
                }
            });
        } else if (_type == ENTERPRISE_NAME) {
            editText.setHint("请输入企业名称");
            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
            customizeTitleView.setTitle("企业名称");
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    txtTip.setText("");
                    if (editText.getText().toString().length() >= 2) {
                        btnOk.setEnabled(true);
                        btnOk.setBackground(getDrawable(R.drawable.bg_button));
                    } else {
                        btnOk.setEnabled(false);
                        btnOk.setBackground(getDrawable(R.drawable.bg_button_disable));
                    }
                }
            });
        } else if (_type == SHOP_NAME) {
            editText.setHint("请输入门店名称");
            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
            customizeTitleView.setTitle("门店名称");
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    txtTip.setText("");
                    if (editText.getText().toString().length() >= 2) {
                        btnOk.setEnabled(true);
                        btnOk.setBackground(getDrawable(R.drawable.bg_button));
                    } else {
                        btnOk.setEnabled(false);
                        btnOk.setBackground(getDrawable(R.drawable.bg_button_disable));
                    }
                }
            });
        } else if (_type == USER_NAME) {
            editText.setHint("请输入名称");
            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(12)});
            customizeTitleView.setTitle("编辑姓名");
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    txtTip.setText("");
                    if (editText.getText().toString().length() >= 2) {
                        btnOk.setEnabled(true);
                        btnOk.setBackground(getDrawable(R.drawable.bg_button));
                    } else {
                        btnOk.setEnabled(false);
                        btnOk.setBackground(getDrawable(R.drawable.bg_button_disable));
                    }
                }
            });
        } else if (_type == IP) {
            editText.setHint("请输入IP地址");
            editText.setText(Constant.URL);
            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(50)});
            customizeTitleView.setTitle("编辑IP");
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    txtTip.setText("");
                    if (editText.getText().toString().length() >= 2) {
                        btnOk.setEnabled(true);
                        btnOk.setBackground(getDrawable(R.drawable.bg_button));
                    } else {
                        btnOk.setEnabled(false);
                        btnOk.setBackground(getDrawable(R.drawable.bg_button_disable));
                    }
                }
            });
        }


        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (_type == IP) {
                    Constant.URL = editText.getText().toString();
                    PrefUtils.setSharedPrefStringData(mContext, PrefUtils.url, Constant.URL);
                    finish();
                } else if (_type == INVIEW_CODE && !editText.getText().toString().equals(strOldInviteCode)) {
                    inputInviteCode();
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("val", editText.getText().toString());
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });

    }

    private void inputInviteCode() {
        HttpManager.setInviteCode(mContext, editText.getText().toString(), new BasicHttp.IMyCallBack() {
            @Override
            public void onResponse(Object resonseObj) {
                HttpManager.getBranch(mContext, new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(Object resonseObj) {
                        JSONArray jsonArray = (JSONArray) resonseObj;
                        if (jsonArray.size() == 0) {

                        } else {
                            HttpManager.selectWhichBranch(mContext, jsonArray.getJSONObject(jsonArray.size() - 1).getString("id"), new BasicHttp.IMyCallBack() {
                                @Override
                                public void onResponse(Object seleltBranchObj) {
                                    showToast("邀请码输入成功");
                                    Intent intent = new Intent(mContext, MainActivity.class);
                                    startActivity(intent);
                                }

                                @Override
                                public void onFailure(String mes) {
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String mes) {

                    }
                });


            }

            @Override
            public void onFailure(String mes) {
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();

    }
}
